package com.fashion.web.actions.gallery;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.fashion.model.gallery.Album;
import com.fashion.model.gallery.GalleryColumn;
import com.fashion.service.gallery.AlbumService;
import com.fashion.service.gallery.GalleryColumnService;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@ParentPackage("json-default")
@Namespace("/gallery")
public class GalleryAction extends ActionSupport {
	@Resource(name = "galleryColumnService")
    private GalleryColumnService galleryColumnService;
	@Resource(name = "albumService")
	private AlbumService albumService;
	private List<Album> albums = new ArrayList<Album>();
	private List<GalleryColumn> columns = new ArrayList<GalleryColumn>();
	private List<Album> rollAlbums;
	private List<Album> recommendAlbums;
	private List<Album> hotAlbums;
	private int pageNum = 1;
	private int rows = 21;

	public List<Album> getRecommendAlbums() {
        return recommendAlbums;
    }

    public List<Album> getRollAlbums() {
        return rollAlbums;
    }
    
    @Action(value = "index", results = {@Result(name = SUCCESS, location = "gallery.jsp")})
    public String index() throws Exception {
    	columns = galleryColumnService.format(galleryColumnService.findAll());
    	recommendAlbums = albumService.find(null, null, "priority", 0, 8);
    	albums = albumService.find(null, null, "priority", rows*(pageNum - 1 ), rows);
    	return SUCCESS;
    }
    
    @Action(value = "find_galleries", results = {@Result(name = SUCCESS, type = "json", params = {"root", "albums", "contentType", "text/html","noCache","true","ignoreHierarchy","false"})})
    public String findGalleries() throws Exception {
    	albums = albumService.find(null, null, "priority", rows*(pageNum - 1 ), rows);
    	return SUCCESS;
    }
	
	private List<GalleryColumn> getAllChildren(GalleryColumn galleryColumn){
	    List<GalleryColumn> list = new ArrayList<GalleryColumn>();
	    List<GalleryColumn> children = galleryColumn.getChildren();
	    if (children != null)
	        for(int i = 0; i < children.size(); i++){
	            list.add(children.get(i));
	            list.addAll(getAllChildren(children.get(i)));
	        }
	    return list;
	}

	public List<Album> getHotAlbums() {
		return hotAlbums;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public List<Album> getAlbums() {
		return albums;
	}

	public List<GalleryColumn> getColumns() {
		return columns;
	}
	
}
